package queue.my_queue;

public class MyQueue {
    static class ListNode {
        ListNode prev;
        ListNode next;
        int val;

        public ListNode(int val) {
            this.val = val;
        }
    }

    private ListNode head;
    private ListNode last;
    private int useSize;
    public boolean offer(int val) {
        ListNode node = new ListNode(val);
        if(head == null) {
            last = head = node;
        } else {
            last.next = node;
            node.prev = last;
            last = node;
        }
        useSize++;
        return true;
    }

    public int poll() {
        if(head == null) {
            return -1;
        }
        int val = head.val;
        if(head == last) {
            head = last = null;
            return val;
        }
        head = head.next;
        head.prev = null;
        return val;
    }

    public int peek() {
        if(head == null) {
            return -1;
        }
        return head.val;
    }

    public int size() {
        return useSize;
    }

    public boolean isEmpty() {
        return head == null;
    }
}
